<template>
  <div>
    <el-row>
      <company-information :uuid="$route.query.id"/>
    </el-row>
    <br>
    <el-card>
      <el-container>
        <el-header>
          <!--          添加补贴弹窗-->
          <el-dialog
              :visible.sync="visible.addVisible"
              title="添加补贴"
              width="30%">

            <subsidy-form @success="querySubsidy(); visible.addVisible = false;"/>

          </el-dialog>
          <!--          修改补贴弹窗-->
          <el-dialog
              :visible.sync="visible.updateVisible"
              title="修改补贴"
              width="30%">
            <subsidy-form :selectSubsidy="selectSubsidy" @success="querySubsidy(); visible.updateVisible = false;"/>
          </el-dialog>
          <el-row>
            <el-col :span="12">
              <h3>补贴列表</h3>
            </el-col>
            <el-col :span="12">
              <div style="text-align: right">
                <el-button v-if="$route.query.id" plain type="success" @click="visible.addVisible = true">添加补贴
                </el-button>
                <el-button plain type="primary" @click="editSubsidy()">修改补贴</el-button>
                <el-button plain type="danger" @click="delSubsidy()">删除补贴</el-button>
              </div>
            </el-col>
          </el-row>

        </el-header>
        <el-main>
          <el-table ref="multipleTable1"
                    :data="subsidyData"
                    @select="select"
                    @select-all="selectAll"
                    @selection-change="selects"
          >
            <el-table-column label="补贴" prop="name"></el-table-column>
            <el-table-column label="补贴金额" prop="price"></el-table-column>
            <el-table-column label="时间" prop="timestamp.timeString"></el-table-column>
            <el-table-column type="selection"></el-table-column>
          </el-table>
          <el-pagination
              :current-page.sync="paging.page"
              :page-size.sync="paging.size"
              :total="subsidyTotal"
              background
              layout="prev, pager, next,jumper"
              @current-change="querySubsidy"
              style="margin-top: 20px"
          >
          </el-pagination>
        </el-main>
      </el-container>
    </el-card>
    <br>
    <el-card>
      <el-container>
        <el-header>
          <!--          添加专利弹窗-->
          <el-dialog :visible.sync="visible.addSubsidyVisible" title="添加专利"
                     width="30%">
            <add-patent-dialog :uuid="$route.query.id" @success="visible.addSubsidyVisible = false; queryPatent()"/>
          </el-dialog>
          <!--          修改专利弹窗-->
          <el-dialog :visible.sync="visible.editSubsidyVisible" title="修改专利"
                     width="30%">

            <modify-patent-component :selectPatent="selectPatent"
                                     @success="visible.editSubsidyVisible = false; queryPatent()"/>

          </el-dialog>


          <el-row>
            <el-col :span="12">
              <h3>专利列表</h3>
            </el-col>
            <el-col :span="12">
              <div style="text-align: right">
                <el-button v-if="$route.query.id" plain type="success" @click="visible.addSubsidyVisible = true">添加专利
                </el-button>
                <el-button plain type="primary" @click="editPatent">修改专利</el-button>
                <el-button plain type="danger" @click="delectPatent">删除专利</el-button>
              </div>
            </el-col>
          </el-row>


        </el-header>
        <el-main>
          <el-table :data="patentData" @select="select1"
                    @selection-change="patentChange"
                    ref="multipleTable"
                    @select-all="selectAll1"
          >
            <el-table-column label="专利名称" prop="name"></el-table-column>
            <el-table-column label="专利状态" prop="status"></el-table-column>
            <el-table-column label="专利类型	" prop="type"></el-table-column>
            <el-table-column label="专利内容" prop="description"></el-table-column>
            <el-table-column label="专利号" prop="id"></el-table-column>
            <el-table-column type="selection"></el-table-column>
          </el-table>
          <el-pagination
              :current-page.sync="paging.page"
              :page-size.sync="paging.size"
              :total="patentTotal"
              background
              layout="prev, pager, next,jumper"
              @current-change="queryPatent"
              style="margin-top: 20px"
          >
          </el-pagination>
        </el-main>
      </el-container>
    </el-card>
  </div>
</template>
<script>
import {copyObj} from "../../../common/utils";
import CompanyInformation from "./companyInformation";
import AddPatentDialog from "../../../components/patentComponent/addPatentForm";
import ModifyPatentComponent from "../../../components/patentComponent/editPatentForm";
import {deletePatent, QueryPatent} from "../../../network/patentApi";
import {deleteSubsidy, QuerySubsidy, UpdateSubsidy} from "../../../network/subsidyApi";
import SubsidyForm from "./subsidy/subsidyForm";

export default {
  name: "companyPage",
  components: {SubsidyForm, ModifyPatentComponent, AddPatentDialog, CompanyInformation},
  data() {
    return {
      visible: {
        addVisible: false,
        updateVisible: false,
        addSubsidyVisible: false,
        editSubsidyVisible: false,
      },
      subsidyData: null,
      patentData: null,
      paging: {
        page: 1,
        size: 20,
        condition: {
          companyUuid: null,
        },
      },
      patentTotal: null,
      subsidyTotal: null,
      selectPatent: {},
      selectSubsidy: {},
    }
  },
  mounted() {
    this.queryPatent();
    this.querySubsidy();

  },
  methods: {

    //修改专利
    editPatent() {
      if (this.selectPatent.uuid) {
        this.visible.editSubsidyVisible = true
      }
      if (!this.selectPatent.uuid) {
        this.$message({
          message: "请勾选专利",
          type: "error"
        })
      }
    },

    //修改补贴
    editSubsidy() {
      if (this.selectSubsidy.uuid) {
        this.visible.updateVisible = true
      }
      if (!this.selectSubsidy.uuid) {
        this.$message({
          message: "请勾选补贴",
          type: "error"
        })
      }
    },

    //选择补贴
    selects(e) {
      console.log(e)
      this.selectSubsidy = copyObj(e[0], {})
    },
    //选择专利
    patentChange(e) {
      console.log(e)
      this.selectPatent = e[0] ? copyObj(e[0]) : {};
    },
    delectPatent() {
      if (!this.selectPatent.uuid) {
        this.$message({
          message: "请勾选专利",
          type: "error"
        })
      } else {
        this.$confirm('此操作将永久删除该公司所有数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deletePatent(this.selectPatent.uuid).then(res => {
            if (res.code === 2000) {
              this.queryPatent();
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    },
    //删除补贴
    delSubsidy() {
      if (!this.selectSubsidy.uuid) {
        this.$message({
          message: "请勾选补贴",
          type: "error"
        })
      } else {
        this.$confirm('此操作将永久删除该补贴, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteSubsidy(this.selectSubsidy.uuid).then(res => {

            if (res.code === 2000) {
              this.querySubsidy();
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    },
    //修改补贴
    addsubsidy() {
      UpdateSubsidy(this.selectSubsidy).then(res => {
        if (res.code === 2000) {
          this.querySubsidy();
          this.visible.updateVisible = false;
        }
      })
    },
    //查询跳转公司专利
    queryPatent() {
      this.paging.condition.companyUuid = this.$route.query.id;
      QueryPatent(this.paging).then(res => {
        let d = res.data;
        this.patentData = d.records;
        this.patentTotal = res.data.total;
      })
    },
    //查询跳转公司补贴
    querySubsidy() {
      this.paging.condition.companyUuid = this.$route.query.id;
      QuerySubsidy(this.paging).then(res => {
        let d = res.data;
        this.subsidyData = d.records;
        this.subsidyTotal = res.data.total;
      })
    },
    select(selection) {
      if (selection.length > 1) {
        let del_row = selection.shift()
        this.$refs.multipleTable1.toggleRowSelection(del_row, false)
      }
    },
    selectAll(selection) {
      if (selection.length > 1) {
        selection.length = 1
      }
    },

    select1(selection) {
      if (selection.length > 1) {
        let del_row = selection.shift()
        this.$refs.multipleTable.toggleRowSelection(del_row, false)
      }
    },
    selectAll1(selection) {
      if (selection.length > 1) {
        selection.length = 1
      }
    },

  },

}
</script>

<style scoped>

</style>